package com.shuda.business.distribution.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuda.business.distribution.entity.DistributionRuleEntity;
import com.shuda.business.distribution.entity.DistributionSchemeEntity;
import com.shuda.business.distribution.mapper.DistributionRuleMapper;
import com.shuda.business.distribution.mapper.DistributionSchemeMapper;
import com.shuda.business.distribution.service.DistributionService;
import com.shuda.common.dto.DistributionRuleDTO;
import com.shuda.common.dto.DistributionSchemeDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分发方案管理服务实现类
 */
@Service
@RequiredArgsConstructor
public class DistributionServiceImpl extends ServiceImpl<DistributionSchemeMapper, DistributionSchemeEntity> implements DistributionService {

    private final DistributionRuleMapper distributionRuleMapper;

    @Override
    public IPage<DistributionSchemeEntity> getSchemeList(Integer pageNum, Integer pageSize, String schemeName, Integer status) {
        Page<DistributionSchemeEntity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DistributionSchemeEntity> wrapper = new LambdaQueryWrapper<>();
        
        if (schemeName != null && !schemeName.isEmpty()) {
            wrapper.like(DistributionSchemeEntity::getSchemeName, schemeName);
        }
        if (status != null) {
            wrapper.eq(DistributionSchemeEntity::getStatus, status);
        }
        
        wrapper.orderByDesc(DistributionSchemeEntity::getCreatedAt);
        return page(page, wrapper);
    }

    @Override
    public DistributionSchemeEntity getSchemeById(Long id) {
        return getById(id);
    }

    @Override
    @Transactional
    public Boolean createScheme(DistributionSchemeDTO schemeDTO) {
        DistributionSchemeEntity schemeEntity = new DistributionSchemeEntity();
        BeanUtils.copyProperties(schemeDTO, schemeEntity);
        schemeEntity.setCreatedAt(LocalDateTime.now());
        schemeEntity.setUpdatedAt(LocalDateTime.now());
        return save(schemeEntity);
    }

    @Override
    @Transactional
    public Boolean updateScheme(Long id, DistributionSchemeDTO schemeDTO) {
        DistributionSchemeEntity schemeEntity = getById(id);
        if (schemeEntity == null) {
            return false;
        }
        
        BeanUtils.copyProperties(schemeDTO, schemeEntity);
        schemeEntity.setUpdatedAt(LocalDateTime.now());
        return updateById(schemeEntity);
    }

    @Override
    @Transactional
    public Boolean deleteScheme(Long id) {
        // 删除相关的规则
        LambdaQueryWrapper<DistributionRuleEntity> ruleWrapper = new LambdaQueryWrapper<>();
        ruleWrapper.eq(DistributionRuleEntity::getSchemeId, id);
        distributionRuleMapper.delete(ruleWrapper);
        
        return removeById(id);
    }

    @Override
    @Transactional
    public Boolean updateSchemeStatus(Long id, Integer status) {
        DistributionSchemeEntity schemeEntity = getById(id);
        if (schemeEntity == null) {
            return false;
        }
        
        schemeEntity.setStatus(status);
        schemeEntity.setUpdatedAt(LocalDateTime.now());
        return updateById(schemeEntity);
    }

    @Override
    public IPage<DistributionRuleEntity> getRuleList(Integer pageNum, Integer pageSize, Long schemeId, String ruleName) {
        Page<DistributionRuleEntity> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DistributionRuleEntity> wrapper = new LambdaQueryWrapper<>();
        
        if (schemeId != null) {
            wrapper.eq(DistributionRuleEntity::getSchemeId, schemeId);
        }
        if (ruleName != null && !ruleName.isEmpty()) {
            wrapper.like(DistributionRuleEntity::getRuleName, ruleName);
        }
        
        wrapper.orderByDesc(DistributionRuleEntity::getCreatedAt);
        return distributionRuleMapper.selectPage(page, wrapper);
    }

    @Override
    public DistributionRuleEntity getRuleById(Long id) {
        return distributionRuleMapper.selectById(id);
    }

    @Override
    @Transactional
    public Boolean createRule(DistributionRuleDTO ruleDTO) {
        DistributionRuleEntity ruleEntity = new DistributionRuleEntity();
        BeanUtils.copyProperties(ruleDTO, ruleEntity);
        ruleEntity.setCreatedAt(LocalDateTime.now());
        ruleEntity.setUpdatedAt(LocalDateTime.now());
        return distributionRuleMapper.insert(ruleEntity) > 0;
    }

    @Override
    @Transactional
    public Boolean updateRule(Long id, DistributionRuleDTO ruleDTO) {
        DistributionRuleEntity ruleEntity = distributionRuleMapper.selectById(id);
        if (ruleEntity == null) {
            return false;
        }
        
        BeanUtils.copyProperties(ruleDTO, ruleEntity);
        ruleEntity.setUpdatedAt(LocalDateTime.now());
        return distributionRuleMapper.updateById(ruleEntity) > 0;
    }

    @Override
    @Transactional
    public Boolean deleteRule(Long id) {
        return distributionRuleMapper.deleteById(id) > 0;
    }

    @Override
    public List<DistributionRuleEntity> getRuleOptions(Long schemeId) {
        LambdaQueryWrapper<DistributionRuleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DistributionRuleEntity::getSchemeId, schemeId);
        wrapper.orderByDesc(DistributionRuleEntity::getCreatedAt);
        return distributionRuleMapper.selectList(wrapper);
    }
}